package dao;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


/**
 * Dao factory for every model
 * 
 * Every Dao class should inherit this one
 * 
 * @author b_aldosa
 * @since May 2013
 * @version $Revision: 1.0 $
 */
public class DaoFactory {

	// Database name
	protected static final String dbName = "db.db";

	// Database path
	protected static String dbPath = System.getProperty("user.dir")
			+ "/persistent/";

	// Connection
	protected static Connection conn;

	/**
	 * Default constructor, creates the database for the first time of app
	 * launch
	 * 
	
	
	 * @since May 9, 2013
	 * @throws SQLException
	 * @throws DaoException */
	public DaoFactory() throws SQLException, DaoException {

		// Creates the persistent folder if it doesn't exist yet
		File file = new File(dbPath);
		if (!file.exists()) {
			file.mkdir();
		}

		// Inits the database
		if (!(new File(dbPath + dbName).exists())) {
			initDb();
			setupTables();
		} else {
			initDb();
		}
	}

	/**
	 * Creates the dabase file
	 * 
	
	 * @throws DaoException
	 * @throws SQLException */
	private void initDb() throws DaoException {
		try {
			Class.forName("org.sqlite.JDBC");
			
		} catch (ClassNotFoundException e) {
			throw new DaoException(e.getMessage());
		}
		try {
			conn = DriverManager
					.getConnection("jdbc:sqlite:" + dbPath + dbName);
		} catch (SQLException e) {
			throw new DaoException(e.getMessage());
		}
	}

	/**
	 * Sets up tables for basic use on the initial startup of the application
	 */
	private void setupTables() {

		Statement statement = null;


		// Create email table
		String sql = "CREATE TABLE emails("
				+ "Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
				+ "ToEmail VARCHAR(50) NOT NULL, "
				+ "FromEmail VARCHAR(50) NOT NULL, " + "Subject VARCHAR(50), "
				+ "Body TEXT, " + "Read BOOLEAN, " + "Ts INTEGER NOT NULL, " + "PendingRecepients VARCHAR(50)"+")";
		try {
			statement = conn.createStatement();
			statement.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}


		// Create meetings table
		sql = "CREATE TABLE meetings("
				+ "Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
				+ "Owner VARCHAR(50) NOT NULL, "
				+ "RevisionId SMALLINT NOT NULL, " 
				+ "Topic VARCHAR(50), "
				+ "Location TEXT, " 
				+ "Start INTEGER NOT NULL, "
				+ "End INTEGER NOT NULL, " 
				+ "Attendees VARCHAR(50), "
				+ "Description TEXT)";
		try {
			statement = conn.createStatement();
			statement.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	/**
	 * Method to returns the EmailDao object
	 * 
	
	 * @return EmailDao object */
	public static EmailDao getEmailDao() {
		return new EmailSqlDao(conn);
	}



	/**
	 * Method to returns the MeetingDao object
	 * 
	
	 * @return MeetingDao object */
	public static MeetingDao getMeetingDao() {
		return new MeetingSqlDao(conn);
	}



}
